require(ggplot2)
require(reshape2)
require(viridis)
require(xtable)

delta <- rho <- seq(0.05, 1, length.out = length.out)

sim_res <- list()
for (i in 1:length(delta)) {
  fn <- paste("./change/corr07/pred/res/sim_pred_change_mse_corr07_", i, ".rds", sep = '')
  sim_res[[i]] <-  readRDS(fn)
}

## data prep for plot
mse_lasso1  <- do.call("cbind", lapply(sim_res, function(x) x[,1]))
mse_lasso2  <- do.call("cbind", lapply(sim_res, function(x) x[,2]))
mse_bridge <- do.call("cbind", lapply(sim_res, function(x) x[,3]))

mse_lasso1_dat  <- data.frame(mse_lasso1)
mse_lasso2_dat  <- data.frame(mse_lasso2)
mse_bridge_dat <- data.frame(mse_bridge)

colnames(mse_lasso1_dat) <- colnames(mse_lasso2_dat) <- colnames(mse_bridge_dat)  <- paste("delta", delta, sep = '')
mse_lasso1_dat$rho  <- rho
mse_lasso2_dat$rho  <- rho
mse_bridge_dat$rho  <- rho

mse_lasso1_dat$method <- "HMM Lasso"
mse_lasso2_dat$method <- "Oracle Lasso"
mse_bridge_dat$method <- "HMBB"


mse_full <- rbind(mse_lasso1_dat, mse_lasso2_dat, mse_bridge_dat)

mse_full <- melt(mse_full, id.vars = c("rho", "method"), variable.name = "delta", value.name = 'L2')
mse_full$delta <- delta[as.numeric(mse_full$delta)]



## mse_full$L21 <- round(mse_full$L2 / 1e2, 3)



## summary
summary.coef <- cbind(with(mse_full, tapply(L2, method, mean)),
                       with(mse_full, tapply(L2, method, sd)))
colnames(summary.coef) <- c("Mean", "SD")
summary.table <- xtable(summary.coef, digits=3)
a <- print(summary.table, booktabs = TRUE)
write(a, file = "SI/Table5_column1.tex")

